package server;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SpringLayout;

/**
 * The Class ServerGUI.
 * In this window the user types the MySql connection details.
 * @author Tomer Elkayam
 */
public class ServerGUI extends JFrame
{
	
	/** swing components. */
	private JPanel serverInfoPanel;
	
	/** The continue button. */
	private JButton continueButton;
	
	/** The cancel button. */
	private JButton cancelButton;
	
	/** The server port text field. */
	private JTextField serverPortTextField;
	
	/** The sql scheme text field. */
	private JTextField sqlSchemeTextField;
	
	/** The sql password text field. */
	private JPasswordField sqlPasswordTextField;
	
	/** The sql username text field. */
	private JTextField sqlUsernameTextField;
	
	/** The port label. */
	private JLabel portLabel;
	
	/** The scheme label. */
	private JLabel schemeLabel;
	
	/** The password label. */
	private JLabel passwordLabel;
	
	/** The username label. */
	private JLabel usernameLabel;
	
	/** The sl_server info panel. */
	private SpringLayout sl_serverInfoPanel;
	
	/**
	 * Instantiates a new server gui.
	 */
	public ServerGUI()
	{
		super();
		
		//set the Frame
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setBounds(1000,700,528,213);
		this.setResizable(false);
		
		//set the panel
		serverInfoPanel = new JPanel();
		
		//initialize text fields and labels
		portLabel = new JLabel("Enter server port number:");
		serverPortTextField = new JTextField(20);
		
		schemeLabel = new JLabel("Enter scheme name:");
		sqlSchemeTextField = new JTextField(20);
		
		usernameLabel = new JLabel("Enter MySql username:");
		sqlUsernameTextField = new JTextField(20);		
		
		passwordLabel = new JLabel("Enter MySql password:");
		sqlPasswordTextField = new JPasswordField(20);
		sl_serverInfoPanel = new SpringLayout();
		sl_serverInfoPanel.putConstraint(SpringLayout.NORTH, sqlUsernameTextField, 5, SpringLayout.SOUTH, sqlPasswordTextField);
		sl_serverInfoPanel.putConstraint(SpringLayout.NORTH, usernameLabel, 3, SpringLayout.NORTH, sqlUsernameTextField);
		sl_serverInfoPanel.putConstraint(SpringLayout.WEST, usernameLabel, 0, SpringLayout.WEST, portLabel);
		sl_serverInfoPanel.putConstraint(SpringLayout.NORTH, schemeLabel, 3, SpringLayout.NORTH, sqlSchemeTextField);
		sl_serverInfoPanel.putConstraint(SpringLayout.WEST, schemeLabel, 0, SpringLayout.WEST, portLabel);
		sl_serverInfoPanel.putConstraint(SpringLayout.NORTH, sqlPasswordTextField, 5, SpringLayout.SOUTH, sqlSchemeTextField);
		sl_serverInfoPanel.putConstraint(SpringLayout.NORTH, passwordLabel, 3, SpringLayout.NORTH, sqlPasswordTextField);
		sl_serverInfoPanel.putConstraint(SpringLayout.WEST, passwordLabel, 0, SpringLayout.WEST, portLabel);
		sl_serverInfoPanel.putConstraint(SpringLayout.NORTH, sqlSchemeTextField, 5, SpringLayout.SOUTH, serverPortTextField);
		sl_serverInfoPanel.putConstraint(SpringLayout.EAST, sqlUsernameTextField, 0, SpringLayout.EAST, serverPortTextField);
		sl_serverInfoPanel.putConstraint(SpringLayout.WEST, sqlPasswordTextField, 0, SpringLayout.WEST, serverPortTextField);
		sl_serverInfoPanel.putConstraint(SpringLayout.WEST, sqlSchemeTextField, 0, SpringLayout.WEST, serverPortTextField);
		sl_serverInfoPanel.putConstraint(SpringLayout.NORTH, serverPortTextField, 5, SpringLayout.NORTH, serverInfoPanel);
		sl_serverInfoPanel.putConstraint(SpringLayout.WEST, serverPortTextField, 213, SpringLayout.WEST, serverInfoPanel);
		sl_serverInfoPanel.putConstraint(SpringLayout.NORTH, portLabel, 8, SpringLayout.NORTH, serverInfoPanel);
		sl_serverInfoPanel.putConstraint(SpringLayout.WEST, portLabel, 23, SpringLayout.WEST, serverInfoPanel);
		serverInfoPanel.setLayout(sl_serverInfoPanel);

		//add components to panel
		serverInfoPanel.add(portLabel);
		serverInfoPanel.add(serverPortTextField);
		serverInfoPanel.add(schemeLabel);
		serverInfoPanel.add(sqlSchemeTextField);
		serverInfoPanel.add(passwordLabel);
		serverInfoPanel.add(sqlPasswordTextField);
		serverInfoPanel.add(usernameLabel);
		serverInfoPanel.add(sqlUsernameTextField);
		serverInfoPanel.add(getContinueButton());
		serverInfoPanel.add(getCancelButton());

		getContentPane().add(serverInfoPanel);
	}
	
	/**
	 * This method initializes StudentsButton.
	 *
	 * @return the continue button
	 */
	private JButton getContinueButton()
	{
		if (continueButton == null) 
		{
			continueButton = new JButton();
			sl_serverInfoPanel.putConstraint(SpringLayout.NORTH, continueButton, 129, SpringLayout.NORTH, serverInfoPanel);
			sl_serverInfoPanel.putConstraint(SpringLayout.WEST, continueButton, 174, SpringLayout.WEST, serverInfoPanel);
			continueButton.setText("continue");
		}
		return continueButton;
	}
	
	/**
	 * Gets the cancel button.
	 *
	 * @return the cancel button
	 */
	private JButton getCancelButton()
	{
		if(cancelButton == null)
		{
			cancelButton = new JButton();
			sl_serverInfoPanel.putConstraint(SpringLayout.NORTH, cancelButton, 129, SpringLayout.NORTH, serverInfoPanel);
			sl_serverInfoPanel.putConstraint(SpringLayout.WEST, cancelButton, 272, SpringLayout.WEST, serverInfoPanel);
			cancelButton.setText("cancel");
		}
		return cancelButton;
	}
	
	/**
	 * Display error message.
	 *
	 * @param errorMessage the error message
	 */
	public void displayErrorMessage(String errorMessage)
	{
		JOptionPane.showMessageDialog(this, errorMessage);
	}
	
	/**
	 * Adds the continue listener.
	 *
	 * @param listenToContinueButton the listen to continue button
	 */
	public void addContinueListener(ActionListener listenToContinueButton)
	{
		continueButton.addActionListener(listenToContinueButton);
	}
	
	/**
	 * Adds the cancle button listener.
	 *
	 * @param listenToCancelButton the listen to cancel button
	 */
	public void addCancleListener(ActionListener listenToCancelButton)
	{
		cancelButton.addActionListener(listenToCancelButton);
	}

	/**
	 * Gets the port field.
	 *
	 * @return the port field
	 */
	public String getPortField()
	{
		return serverPortTextField.getText();
	}
	
	/**
	 * Gets the scheme field.
	 *
	 * @return the scheme field
	 */
	public String getSchemeField()
	{
		return sqlSchemeTextField.getText();
	}
	
	/**
	 * Gets the password field.
	 *
	 * @return the password field
	 */
	public String getPasswordField()
	{
		char[] pass  =  sqlPasswordTextField.getPassword();
		String passString = new String(pass);
		return passString;
	}
	
	/**
	 * Gets the username field.
	 *
	 * @return the username field
	 */
	public String getUsernameField()
	{
		return sqlUsernameTextField.getText();
	}
	
	/**
	 * Clear all fields.
	 */
	public void clearAllFields()
	{
		serverPortTextField.setText("");
		sqlSchemeTextField.setText("");
		sqlPasswordTextField.setText("");
		sqlUsernameTextField.setText("");
	}
}
